package pojo;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Manage {
    public static List getEmployeeList(){
        File loadFile = new File("saveData\\employeeData");
        if(!loadFile.exists()){
            return new ArrayList();
        } else{
            try{
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(loadFile));
                List<Employee> list = (List<Employee>) ois.readObject();
                return list;
            } catch(Exception e){
                throw new RuntimeException(e);
            }
        }
    }
    public static void saveEmployee(List list){
        try{
            File savefile = new File("saveData\\employeeData");
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(savefile));
            oos.writeObject(list);
            oos.flush();
            oos.close();
        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static List getDepartmentList(){
        File loadFile = new File("saveData\\departments");
        if(!loadFile.exists()){
            return new ArrayList();
        } else{
            try{
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(loadFile));
                List<Department> list = (List<Department>) ois.readObject();
                return list;
            } catch(Exception e){
                throw new RuntimeException(e);
            }
        }
    }

    public static void saveDepartment(List list){
        try{
            File savefile = new File("saveData\\departments");
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(savefile));
            oos.writeObject(list);
            oos.flush();
            oos.close();
        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static List getUserList(){
        File loadFile = new File("saveData\\userData");
        if(!loadFile.exists()){
            return new ArrayList();
        } else{
            try{
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(loadFile));
                List<Admin> list = (List<Admin>) ois.readObject();
                return list;
            } catch(Exception e){
                throw new RuntimeException(e);
            }
        }
    }

    public static void adminSaving(List list){
        try{
            File savefile = new File("saveData\\userData");
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(savefile));
            oos.writeObject(list);
            oos.flush();
            oos.close();
        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
